package com.kk.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kk.score.dao.GradeDao;
import com.kk.score.dao.PushGradeDao;
import com.kk.score.entity.GradeEntity;
import com.kk.score.entity.PushGradeEntity;
import com.kk.score.entity.TaskEntity;
import com.kk.score.entity.vo.GradePersonVo;
import com.kk.score.entity.vo.UnitGradeVo;
import com.kk.score.entity.vo.UnitVo;
import com.kk.score.service.PushGradeService;
import com.kk.score.utils.DateUtils;
import com.kk.score.utils.PageUtils;
import com.kk.score.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


@Service("pushGradeService")
public class PushGradeServiceImpl extends ServiceImpl<PushGradeDao, PushGradeEntity> implements PushGradeService {

    @Autowired
    private PushGradeDao pushGradeDao;

    @Autowired
    private GradeDao gradeDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String status = (String) params.get("status");
        String year = (String) params.get("year");
        if (!StringUtils.hasText(year)) {
            year = DateUtils.getYears();
        }
        if (!StringUtils.hasText(status)) {
            status = "1";
        }
        IPage<?> pushPage = pushGradeDao.findPushPage(new Query<UnitGradeVo>().getPage(params), status, year);
        @SuppressWarnings("unchecked")
        List<UnitGradeVo> records = (List<UnitGradeVo>) pushPage.getRecords();

        for (UnitGradeVo record : records) {
            String uid = record.getPushUid();
            //查询item的任务集合
//            List<GradeEntity> gradeEntities = gradeDao.selectList(new LambdaQueryWrapper<GradeEntity>().eq(GradeEntity::getPushUid, uid));
            List<GradePersonVo> gradeEntities = gradeDao.findGradeByPushUid(uid);
            //任务个数
            int count = gradeEntities.size();
            //未完成人数
            int person = 0,scoreS = 0,scoreA = 0,scoreB = 0,scoreC = 0;
            for (GradePersonVo gradeEntity : gradeEntities) {
                record.setStartTime(gradeEntity.getStartTime());
                record.setEndTime(gradeEntity.getEndTime());
                if(!gradeEntity.getDepartmentName().equals("测试单位")
                        && !gradeEntity.getDepartmentUid().equals("1541f07cee52da96d84878a322716103")
                ){
                    if (gradeEntity.getStatus() == 1) {
                        switch (gradeEntity.getScore()){
                            case 4 : ++scoreS;break;
                            case 3 : ++scoreA;break;
                            case 2 : ++scoreB;break;
                            case 1 : ++scoreC;break;
                        }
                    }
                    //若任务中有一个任务的状态不为已完成
                    if (gradeEntity.getStatus() != 1) {
                        person += 1;
                    }
                }
            }
            //设置人数
            record.setPerson(person);
            record.setScoreS(scoreS);
            record.setScoreA(scoreA);
            record.setScoreB(scoreB);
            record.setScoreC(scoreC);
        }
        return new PageUtils(pushPage);
    }
}